#include "stable.h"
#include "QkTocModel.h"

class QkTreeItem
{
public:
	QkTreeItem(const QString &folderName, QkTreeItem *parent = 0)
	{
		m_folderName = folderName;
		m_parentItem = parent;
	}

	~QkTreeItem()
	{
		qDeleteAll(m_childItems);
	}

	void appendChild(QkTreeItem *child)
	{
		m_childItems.append(child);
	}

	QkTreeItem *child(int row)
	{
		return m_childItems[row];
	}

	int childCount() const
	{
		return m_childItems.count();
	}

	int columnCount() const
	{
		return 2;
	}

	QVariant data(int column, int role) const
	{
		Q_UNUSED(column);

		switch(role)
		{
		case Qt::DisplayRole:
			{
			QDir dir(m_folderName);
			return QVariant(dir.dirName());
			}
		case Qt::SizeHintRole:
			{
				if(column == 0)
				{
					return QSize(40, 40);
				}
			}
		case Qt::DecorationRole:
			{
				if(m_icon.isNull())
				{
					QString iconPath = m_folderName + "/icon.gif";
					m_icon.addFile(iconPath);
				}

				return m_icon;
			}
		default:
			break;
		}

		return QVariant();
	}

	int row() const
	{
		if (m_parentItem)
			return m_parentItem->m_childItems.indexOf(const_cast<QkTreeItem*>(this));

		return -1;
	}

	QkTreeItem *parent()
	{
		return m_parentItem;
	}

private:
	QList<QkTreeItem*> m_childItems;
	QkTreeItem *m_parentItem;
	QString m_folderName;
	mutable QIcon m_icon;
};

QkTocModel::QkTocModel() : m_rootItem(NULL)
{
	loadData();
}

QkTocModel::~QkTocModel()
{
	delete m_rootItem;
}

void QkTocModel::loadData()
{
	QTimer::singleShot(0, this, SLOT(doLoadData()));
}

void QkTocModel::doLoadData()
{
	QString appFolder = qApp->applicationDirPath();
	QString dataFolder = appFolder + "/data";

	m_rootItem = new QkTreeItem(dataFolder);

	QDir dir(dataFolder);
	
	QStringList list = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
	foreach(QString folder, list)
	{

		m_rootItem->appendChild(new QkTreeItem(dataFolder + "/" + folder, m_rootItem));
	}

	if(m_rootItem->childCount())
	{
		on_data_loaded();
	}
}

void QkTocModel::on_data_loaded()
{
	emit dataChanged(QModelIndex(), QModelIndex());
}

QModelIndex QkTocModel::index(int row, int column, const QModelIndex &parent) const
{
	if (!hasIndex(row, column, parent))
		return QModelIndex();

	QkTreeItem *parentItem = itemFromIndex(parent);

	QkTreeItem *childItem = parentItem->child(row);
	if (childItem)
		return createIndex(row, column, childItem);

	return QModelIndex();
}

QModelIndex QkTocModel::parent(const QModelIndex &child) const
{
	if (!child.isValid())
		return QModelIndex();

	QkTreeItem *childItem = static_cast<QkTreeItem*>(child.internalPointer());
	QkTreeItem *parentItem = childItem->parent();

	if (parentItem == m_rootItem)
		return QModelIndex();

	return createIndex(parentItem->row(), 0, parentItem);
}

bool QkTocModel::hasChildren (const QModelIndex &parent) const
{
	QkTreeItem *parentItem = itemFromIndex(parent);
	return parentItem->childCount() > 0;
}

int QkTocModel::rowCount(const QModelIndex &parent) const
{
	if (parent.column() > 0)
		return 0;

	QkTreeItem *parentItem = itemFromIndex(parent);
	if(parentItem)
		return parentItem->childCount();

	return 0;
}

int QkTocModel::columnCount(const QModelIndex &parent) const
{
	QkTreeItem *item = itemFromIndex(parent);
	return item->columnCount();
}

QVariant QkTocModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid())
		return QVariant();

	QkTreeItem *item = itemFromIndex(index);
	return item->data(index.column(), role);
}

QVariant QkTocModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
	{
		if(section == 0)
			return QString("Name");

		if(section == 1)
			return QString("Size");
	}

	return QVariant();
}

QkTreeItem *QkTocModel::itemFromIndex(const QModelIndex &index) const
{
	if(index.isValid())
	{
		return static_cast<QkTreeItem*>(index.internalPointer());
	}

	return m_rootItem;
}
